package librus.cheeseChaser.components
{
	import flash.geom.Point;
	
	import starling.display.DisplayObject;
	import starling.display.Image;
	import starling.events.Event;
	import starling.events.EventDispatcher;
	
	import librus.cheeseChaser.tile.Tile;
	
	import librus.cheeseChaser.util.Assets;
	import librus.cheeseChaser.util.Sprite;
	
	public class Holder extends Sprite
	{
		public function Holder()
		{
			super();
		}
		
		private var __firstChild:Tile;
		
		
		public function get firstChild():Tile
		{
			return __firstChild;
		}

		override public function addChild(child:DisplayObject):DisplayObject
		{
			if (child is Tile && !firstChild)
			{
				__firstChild = child as Tile;
				x = __firstChild.x;
				y = __firstChild.y;
				
				__firstChild.x = __firstChild.y = 0;
			}
			
			var i:int;
			var delta:Number; 
			if (child.x < 0)
			{
				delta = child.x;
				x += delta;
				child.x =0;
				for (i=0; i< numChildren; i++)
					getChildAt(i).x -= delta;  
			}
			
			if (child.y < 0)
			{
				delta = child.y;				
				y += delta;
				child.y = 0;
				for (i=0; i< numChildren; i++)
					getChildAt(i).y -= delta;
				
			}
			
			return super.addChild(child);		
		}
			
	}
}